﻿#include <iostream>
#include <sstream>
#include <iomanip>
#include <memory>
#include "SerialPort.h"
#include "DisplayDriver.h"
#include "SystemInfo.h"

using namespace MC500;
using namespace std;

const std::string itos(int a)
{
    ostringstream line;
    line << a;
    return line.str();
}
int main(int argc, char *argv[])
{
    if (argc < 4)
    {
        cout << "usage:" << endl
             << "LCD tty0 eth0 /boot" << endl;
        return -1;
    }

    shared_ptr<SerialPort> uart = SerialPort::Create(argv[1]);
    int32_t ret = uart->Init(38400L);
    if (ret != MC500_SUCCESS)
    {
        return ret;
    }

    shared_ptr<DisplayDriver> driver = make_shared<DisplayDriver>(uart);
    do
    {
        DateTime dateTime = SystemInfo::GetInstance()->GetDateTime();
        ostringstream line;
        line << setw(2) << setfill('0') << (int)dateTime.hour << ":" << setw(2) << setfill('0') << (int)dateTime.min << ":" << setw(2) << setfill('0') << (int)dateTime.sec;
        driver->WriteString(0, 0, line.str().c_str());
        line.str("");
        line << "CPU:" << setw(8) << setfill(' ') << left << std::string() + itos((int)SystemInfo::GetInstance()->GetCPUUsed()) + "%";
        line << "Mem:" << setprecision(2) << 100 * ((double)SystemInfo::GetInstance()->GetPhyMemUsed() / SystemInfo::GetInstance()->GetPhyMem()) << "%";
        line << "                          ";
        //cout << "Writing str " << line.str() <<endl;
        driver->WriteString(0, 16, line.str());
        TrifficInfo trifficInfo = SystemInfo::GetInstance()->GetTrifficInfo(argv[2]);
        std::string upload = trifficInfo.GetUploadSpeed();
        std::string download = trifficInfo.GetDownloadSpeed();
        line.str("");
        line << "Up:" << setw(9) << setfill(' ') << left << upload;
        line << "Down:" << setw(6) << setfill(' ') << left << download;
        driver->WriteString(0, 32, line.str());
        line.str("");
        line << "Disk:[";
        uint8_t diskUsage = SystemInfo::GetInstance()->GetDiskUsage(argv[3]);
        for (int i = 0; i < 10; i++)
        {
            if (i < diskUsage / 10)
            {
                line << '|';
            }
            else
            {
                line << ' ';
            }
        }
        line << "]" << (int)diskUsage << "%";
        driver->WriteString(0, 48, line.str().c_str());
        driver->Refresh(false);

        //sleep(1);

    } while (1);

    return 0;
}